from odoo import fields, models


class ProductionDailyReportLine(models.TransientModel):
    _name = 's.production.daily.report.line'
    _description = 'Production Daily Report Line'
    _order = 'done_create'

    # production_id = fields.Many2one('mrp.production', 'Production')
    product_id = fields.Many2one('product.product', 'Product')
    production_id = fields.Many2one('mrp.production', 'Production')
    partner_id = fields.Many2one('res.partner', 'Partner', compute='_compute_partner_id')
    workcenter_id = fields.Many2one('mrp.workcenter', 'Work Center')
    panel_qty = fields.Float('PANEL Quantity')
    production_area = fields.Float('Production Area')
    scrap_area = fields.Float('Scrap Area')
    done_create = fields.Datetime('Date')

    def _compute_area(self):
        for rec in self:
            uom_values = {x.uom_id.code: x.uom_value for x in rec.product_id.uom_value_ids}
            set_height = uom_values.get('SETL', 0)
            set_width = uom_values.get('SETW', 0)
            rec.production_area = rec.workorder_line_id.qty_produced_set * set_height * set_width / 1000000
            rec.scrap_area = rec.workorder_line_id.qty_reject_set * set_height * set_width / 1000000

    def _compute_partner_id(self):
        for rec in self:
            if rec.production_id.s_sale_order_line_ids:
                sale = rec.production_id.s_sale_order_line_ids[0].order_id
                rec.partner_id = sale.partner_id.id
            else:
                rec.partner_id = None